package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.PageResult;
import com.mall.common.Result;
import com.mall.dto.OrderCreateDTO;
import com.mall.service.OrderService;
import com.mall.util.JwtUtil;
import com.mall.util.TokenUtils;
import com.mall.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单控制器
 * 
 * @author Mall Team
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private TokenUtils tokenUtils;
    
    /**
     * 创建订单（从购物车）
     */
    @PostMapping("/create")
    @AuthCheck
    public Result<String> createOrder(@Valid @RequestBody OrderCreateDTO orderCreateDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        String orderNo = orderService.createOrder(userId, orderCreateDTO);
        return Result.success("订单创建成功", orderNo);
    }
    
    /**
     * 直接下单（不通过购物车）
     */
    @PostMapping("/createDirect")
    @AuthCheck
    public Result<String> createDirectOrder(@Valid @RequestBody OrderCreateDTO.DirectOrderDTO directOrderDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        String orderNo = orderService.createDirectOrder(userId, directOrderDTO);
        return Result.success("订单创建成功", orderNo);
    }
    
    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    @AuthCheck
    public Result<OrderVO.OrderDetailVO> getOrderDetail(@PathVariable Long orderId) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        OrderVO.OrderDetailVO orderDetail = orderService.getOrderDetail(userId, orderId);
        return Result.success(orderDetail);
    }
    
    /**
     * 根据订单号获取订单详情
     */
    @GetMapping("/detail/{orderNo}")
    @AuthCheck
    public Result<OrderVO.OrderDetailVO> getOrderDetailByOrderNo(@PathVariable String orderNo) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        OrderVO.OrderDetailVO orderDetail = orderService.getOrderDetailByOrderNo(userId, orderNo);
        return Result.success(orderDetail);
    }
    
    /**
     * 获取用户订单列表
     */
    @GetMapping("/list")
    @AuthCheck
    public Result<PageResult<OrderVO.OrderListVO>> getUserOrderList(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        PageResult<OrderVO.OrderListVO> pageResult =
            orderService.getUserOrderPage(userId, status, pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 取消订单
     */
    @PutMapping("/cancel/{orderId}")
    @AuthCheck
    public Result<String> cancelOrder(@PathVariable Long orderId) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        orderService.cancelOrder(userId, orderId);
        return Result.success("订单取消成功");
    }
    
    /**
     * 根据订单号取消订单
     */
    @PutMapping("/cancel/orderNo/{orderNo}")
    @AuthCheck
    public Result<String> cancelOrderByOrderNo(@PathVariable String orderNo) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        orderService.cancelOrderByOrderNo(userId, orderNo);
        return Result.success("订单取消成功");
    }
    
    /**
     * 确认收货
     */
    @PutMapping("/confirm/{orderId}")
    @AuthCheck
    public Result<String> confirmOrder(@PathVariable Long orderId) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        orderService.confirmOrder(userId, orderId);
        return Result.success("确认收货成功");
    }
    
    /**
     * 删除订单
     */
    @DeleteMapping("/{orderId}")
    @AuthCheck
    public Result<String> deleteOrder(@PathVariable Long orderId) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        orderService.deleteOrder(userId, orderId);
        return Result.success("订单删除成功");
    }
    
    /**
     * 获取订单统计信息
     */
    @GetMapping("/statistics")
    @AuthCheck
    public Result<OrderService.OrderStatisticsVO> getOrderStatistics() {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        OrderService.OrderStatisticsVO statistics = orderService.getOrderStatistics(userId);
        return Result.success(statistics);
    }
}